#include <iostream>
#include <ctime>
#include <vector>

using std::vector;

void quicksort(vector<int>& vec,int low,int high)
{
	if(low >= high)
		return;
	int i = low;
	int j = high;
	int tmp = vec[i];

	while(i < j)
	{
		//从右侧向左遍历，直到找到比tmp小的数，赋值给vec[i]
		while(i < j && vec[j] >= tmp)
			j--;
		vec[i] = vec[j];
		//从左侧向右遍历，直到找到比tmp大的数，赋值给vec[j]
		while(i < j && vec[i] <= tmp)
			i++;
		vec[j] = vec[i];
	}
	vec[i] = tmp;

	//vec[i]已经稳定了，不需要再比较
	quicksort(vec,low,i-1);
	quicksort(vec,i+1,high);
}

int main()
{
	vector<int> vec;
	const int n = 20;//数组大小
	srand(time(0));
	for (int i = 0; i < n; ++i) {
		vec.push_back(rand()%100+1);
	}
	std::cout << "before sort:" << std::endl;
	for (int ele : vec) {
		std::cout << ele << ",";
	}
	std::cout << std::endl;

	quicksort(vec,0,n-1);
	std::cout << "after sort:" << std::endl;
	for (int ele : vec) {
		std::cout << ele << ",";
	}
	std::cout << std::endl;
	return 0;
}
